package com.dnjn.happiness.emo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.SysJobVO;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.domain.UserRecordTask;
import com.dnjn.happiness.emo.domain.vo.UserRecordTaskVO;
import com.dnjn.happiness.emo.mapper.UserRecordTaskMapper;
import com.dnjn.happiness.emo.service.IUserRecordTaskService;
import com.dnjn.happiness.job.api.RemoteJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 任务派发userRecordTaskService业务层处理
 *
 * @author amber
 * @date 2024-01-12
 */
@Service
public class UserRecordTaskServiceImpl extends ServiceImpl<UserRecordTaskMapper, UserRecordTask> implements IUserRecordTaskService {

    @Autowired
    private RemoteJobService jobService;

    @Override
    public Page<UserRecordTaskVO> listByAll(String userNameOrTaskContent,Long userId, Page page) {
        return baseMapper.listByAll(userNameOrTaskContent,userId, page);
    }

    @Override
    public Page<UserRecordTaskVO> listByAppraise(String userNameOrTaskContent,Long userId, Page page) {
        return baseMapper.listByAppraise(userNameOrTaskContent,userId, page);
    }

    @Override
    public List<UserRecordTaskVO> selectX(Long taskId) {
        return baseMapper.selectX(taskId);
    }

    @Override
    public UserRecordTaskVO selectByTaskId(Long taskId, Long userId) {
        return baseMapper.selectByTaskId(taskId, userId);
    }
    @Override
    public void timedOut(UserRecordTask domain) throws Exception {
        String dayOfYear = String.valueOf(domain.getDateDeadline().getYear());//年
        String month = String.valueOf(domain.getDateDeadline().getMonthValue());//月
        // 今天是周几（返回的是个枚举类型，需要再getValue()）
        String dayofWeek = String.valueOf(domain.getDateDeadline().getDayOfWeek().getValue());//周几
        String dayofMonth = String.valueOf(domain.getDateDeadline().getDayOfMonth());//几号
//        String hour = String.valueOf(domain.getDateDeadline().getHour());//小时
//        String minute = String.valueOf(domain.getDateDeadline().getMinute());//分钟
//        String second = String.valueOf(domain.getDateDeadline().getSecond());//秒

        String cronStr = 59 + " " + 59 + " " + 23 + " " + dayofMonth + " " + month + " " + "?" + " " + dayOfYear;
        SysJobVO job = new SysJobVO();
        job.setJobGroup("taskHandler");
        job.setJobName("taskHandler" + domain.getTaskId().toString());
        job.setCronExpression(cronStr);
        job.setInvokeTarget("taskHandler.theTimedOut('" + domain.getTaskId().toString() + "')");
//            job.setInvokeTarget("workforceTask.handleWorkforceshift");
        job.setCreateBy(SecurityUtils.getUsername());
        job.setMisfirePolicy("3");
        job.setStatus("0");
        jobService.insertJob(job, SecurityConstants.INNER);
        job.setInvokeTarget("taskHandler.theTimedOut('" + domain.getTaskId().toString() + "','" + job.getJobId().toString() + "' )");
        jobService.updateJob(job, SecurityConstants.INNER);
        SysJobVO newJob = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(job.getJobId(), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            newJob = result.getData();
        }
        newJob.setStatus("0");
        jobService.changeStatusApi(newJob, SecurityConstants.INNER);
    }

    @Override
    public UserRecordTaskVO selectByTaskIdByUser(Long taskId, Long userId) {
        return baseMapper.selectByTaskIdByUser(taskId, userId);
    }

    @Override
    public Page<UserRecordTaskVO> listBrokenDown(String userNameOrTaskContent, Long userId, Page page) {
        return baseMapper.listBrokenDown(userNameOrTaskContent,userId,page);
    }


    @Override
    public Page<UserRecordTaskVO> listByMy(String taskStatus, String taskContent, Long userId, Page page) {
        return baseMapper.listByMy(taskStatus, taskContent, userId, page);
    }


}
